package com.spzx.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.product.api.domain.Category;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.dto.*;
import com.spzx.product.api.domain.Product;
import com.spzx.product.domain.ProductDetails;
import com.spzx.product.domain.SkuStock;
import com.spzx.product.service.*;
import com.spzx.product.mapper.ProductSkuMapper;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【product_sku(商品sku)】的数据库操作Service实现
* @createDate 2024-07-24 20:11:40
*/
@Service
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, ProductSku>
    implements ProductSkuService{

    @Resource
    CategoryService categoryService;
    @Resource
    SkuStockService skuStockService;
    @Lazy
    @Resource
    ProductService productService;
    @Resource
    ProductDetailsService productDetailsService;
    @Override
    public IndexHotData getIndexHotData() {
        //1、查询一级分类的数据
        List<Category> categories = categoryService.list(Wrappers.lambdaQuery(Category.class)
                .eq(Category::getParentId, 0));

        //2、查询畅销商品的列表  前六条
        List<ProductSku> productSkus = baseMapper.selectHotSkus();
        
        IndexHotData indexHotData = new IndexHotData();
        indexHotData.setCategoryList(categories);
        indexHotData.setProductSkuList(productSkus);
        return indexHotData;
    }

    @Override
    public Page<ProductSku> listSkus(SkuSearchDTO skuSearchDTO) {
        Page<ProductSku> page=new Page<ProductSku>(skuSearchDTO.getPageNo(),skuSearchDTO.getPageSize());
        List<ProductSku> skus = baseMapper.selectSkus(page,skuSearchDTO);
        page.setRecords(skus);
        return page;
    }

    @Override
    public ProductSkuDTO getSkuDTOById(Long id) {
        //1、根据skuId查询sku详情
        //product_sku表:销售价格  市场价格  sku标题   sku的规格值属性
        ProductSku productSku = this.getOne(Wrappers.lambdaQuery(ProductSku.class)
                .eq(ProductSku::getId, id)
                .eq(ProductSku::getStatus, 1));
        if(productSku==null){
            throw new ServiceException("skuId不存在");
        }

        //2、根据skuId查询商品分类
        SkuStock skuStock = skuStockService.getOne(Wrappers.lambdaQuery(SkuStock.class)
                .eq(SkuStock::getSkuId, id)
                .last("limit 1"));

        //3、根据sku查询到的productId查询商品详情
        Long productId = productSku.getProductId();
        Product product = productService.getById(productId);

        //4、根据sku查询到的productId查询详情图列表
        ProductDetails productDetails = productDetailsService.getOne(Wrappers.lambdaQuery(ProductDetails.class)
                .eq(ProductDetails::getProductId, productId));

        //5、根据sku查询到的productId查询spu下的sku列表
        List<ProductSku> productSkus = this.list(Wrappers.lambdaQuery(ProductSku.class)
                .eq(ProductSku::getProductId, productId)
                .eq(ProductSku::getStatus, 1));

        //6、组装数据
        ProductSkuDTO productSkuDTO = new ProductSkuDTO();

        productSkuDTO.setProductSku(productSku);
        productSkuDTO.setProduct(product);
        //组装sku价格数组
        SkuPriceDTO skuPrice = new SkuPriceDTO();
        skuPrice.setSkuId(id);
        skuPrice.setSalePrice(productSku.getSalePrice());
        skuPrice.setMarketPrice(productSku.getMarketPrice());
        productSkuDTO.setSkuPrice(skuPrice);

        //轮播图
        List<String> slidaUrlList = StringUtils.isEmpty(product.getSliderUrls()) ? null : Arrays.asList(product.getSliderUrls().split(","));
        productSkuDTO.setSliderUrlList(slidaUrlList);

        //详情图
        List<String> detailImageUrlList = StringUtils.isEmpty(productDetails.getImageUrls()) ? null : Arrays.asList(productDetails.getImageUrls().split(","));
        productSkuDTO.setDetailsImageUrlList(detailImageUrlList);

        //规格值列表
        List<SpecValueDTO> specValueDTOS = JSON.parseArray(product.getSpecValue(), SpecValueDTO.class);
        productSkuDTO.setSpecValueList(specValueDTOS);

        //sku库存信息
        SkuStockDTO skuStockDTO = new SkuStockDTO();
        BeanUtils.copyProperties(skuStock,skuStockDTO);
        productSkuDTO.setSkuStockVo(skuStockDTO);

        //一个product下所有的sku值的规格属性值 和 该skuid的映射

        Map<String, Long> skuSpecValueMap = productSkus.stream().collect(Collectors.toMap(ProductSku::getSkuSpec, ProductSku::getId));
        productSkuDTO.setSkuSpecValueMap(skuSpecValueMap);

        return productSkuDTO;
    }

}




